import 'package:flutter/material.dart';
import 'package:robot3/widgets.dart';

/// showMenu的使用
void main() {
  final themeData = ThemeData(
    popupMenuTheme: PopupMenuThemeData(
      menuPadding: EdgeInsets.zero,
      color: Colors.tealAccent,
      // 菜单背景颜色
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(0), // 菜单圆角
      ),
      elevation: 0,
      // 菜单阴影
      textStyle: TextStyle(
        fontSize: 4,
        color: Colors.yellow, // 菜单文字样式
      ),
    ),
  );

  runApp(MaterialApp(theme: themeData, home: const SecondScreen()));
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('Second Screen'),
      // ),
      body: Column(
        children: [
          SizedBox(height: 50),
          Row(
            children: [
              RobotButton(onClick: (text) {}),
              SizedBox(width: 200),
              Test01(),
            ],
          ),
        ],
      ),
    );
  }
}

class Test01 extends StatelessWidget {
  const Test01({super.key});

  @override
  Widget build(BuildContext context) {
    return RobotButton(
      onClick: (text) {
        final RenderBox button = context.findRenderObject() as RenderBox;
        final Offset position = button.localToGlobal(Offset.zero);
        showMenu(
          menuPadding: EdgeInsets.zero,
          position: RelativeRect.fromLTRB(
            position.dx,
            position.dy + button.size.height - 10,
            position.dx + button.size.width,
            0,
          ),
          context: context,
          items: buildItems(),
        );
      },
    );
  }
}

final map = {
  "关于": Icons.info_outline,
  "帮助": Icons.help_outline,
  "问题反馈": Icons.add_comment,
};

List<PopupMenuItem<String>> buildItems() {
  return map.keys
      .toList()
      .map(
        (e) => PopupMenuItem<String>(
          height: 1, // 设置菜单项的高度
          padding: EdgeInsets.zero, // 移除 PopupMenuItem 的内边距
          value: e,
          child: SizedBox(height: 20, child: Text(e)),
        ),
      )
      .toList();
}
